package com.sfac.modules.test.mapper;

import com.sfac.modules.common.vo.Search;
import com.sfac.modules.test.pojo.City;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface CityMapper {

    /**
     * 添加城市信息
     * @param city
     */
    @Insert("insert into test_city (city_id,city_name,local_city_name" +
            ",country_id,district,population,date_modified,date_created)values(#{cityId},#{cityName},#{localCityName}" +
            ",#{countryId},#{district},#{population},#{dateModified},#{dateCreated})")
    // 将插入的数据的id返回
    @Options(useGeneratedKeys = true,keyProperty = "cityId",keyColumn = "city_id")
    void insertCity(City city);

    /**
     * 查询是否重复
     * @param cityName
     * @return
     */
    @Select("select * from test_city where city_name = #{cityName}")
    City getCityByName(String cityName);



    /**
     * 修改城市信息
     * @param city
     */
    @Update("update test_city set city_name = #{cityName},local_city_name = #{localCityName}," +
            "country_id = #{countryId},district = #{district},population = #{population},date_modified = #{dateModified}," +
            "date_created = #{dateCreated} where city_id = #{cityId}")
    void updateCity(City city);


    /**
     * 删除城市
     * @param cityId
     */
    @Delete("delete from  test_city where city_id = #{cityId}")
    int deleteCity(int cityId);


    @Select("select city_id,city_name,local_city_name"  +
            ",country_id,district,population,date_modified,date_created from test_city where city_id = #{cityId}")
    City selectCityByCityId(int cityId);

    @Select("select city_id,city_name,local_city_name"  +
            ",country_id,district,population,date_modified,date_created from test_city")
    List<City> selectAllCitys();

    @Select("select * from test_city where city_name = #{cityName} and local_city_name = #{localCityName}")
    City getByCityName(@Param("cityName") String cityName,@Param("localCityName") String localCityName);

    /**
     * 根据国家ID查询
     * @param countryId
     * @return
     */
    @Select("select * from test_city where country_id = #{countryId}")
    List<City> selectCityByCountryId(String countryId);


    /**
     * 分页查询
     * @param search
     * @return
     */
    @Select("<script>"
            + "select * from test_city "
            + "<where> "
            + "<if test='keyWord != \"\" and keyWord != null'>"
            + " and (city_name like '%${keyWord}%' or "
            + " local_city_name like '%${keyWord}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='sort != \"\" and sort != null'>"
            + " order by ${sort} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by city_id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<City> getCitiesBySearch(Search search);
}
